package com.gadgets.busi.bm03;

import com.gadgets.framework.common.util.FileOperUtil;
import com.gadgets.framework.common.util.StringUtils;

import java.io.*;
import java.util.Stack;

/**
 * 功能说明：
 * 软件版权：恒生电子股份有限公司
 * 方法行数扫描
 * @author xiujunqing
 * @version V202101.00.000
 * @data 2022-04-12
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-04-12  xiujunqing   创建文件
 */
public class FunctionLineNumber {

    private static final int LIMIT = 25;

    public static void main(String[] args) {
        File root = new File("F:\\Code\\Hundsun\\cpes4.0");
        FunctionLineNumber.FileLoad(root);
    }

    public static void FileLoad(File rootPath) {
        if (rootPath.isDirectory()) {
            if(rootPath.getPath().contains(".idea") || rootPath.getPath().contains(".svn") || rootPath.getPath().contains("target")){
                return;
            }
            File[] sonFile = rootPath.listFiles();
            for (File son : sonFile) {
                FileLoad(son);
            }
        } else {
            System.out.println("---------------------beg-----------------------");
            FileOperUtil.writeTxtTotFile("D:\\log.txt","---------------------beg-----------------------\n");
            System.out.println("文件:" + rootPath);
            FileOperUtil.writeTxtTotFile("D:\\log.txt","文件:" + rootPath + "\n");
            if(rootPath.getName().endsWith(".java")) {
                BufferedReader bufferedReader = null;
                FileInputStream inputStream = null;
                InputStreamReader inputStreamReader = null;
                try {
                    inputStream = new FileInputStream(rootPath);
                    inputStreamReader = new InputStreamReader(inputStream);
                    bufferedReader = new BufferedReader(inputStreamReader);
                    int i = 0;
                    Stack<String> stringStack = new Stack<>();
                    boolean calculateFlag = false;
                    String text = null;
                    while (null != (text = bufferedReader.readLine())) {
                        if(StringUtils.isBlank(text.trim()) || text.trim().startsWith("//"))
                            continue;
                        if (text.contains("{")) {
                            stringStack.push("{");
                            if (stringStack.size() == 2 && !calculateFlag) {
                                // start calculate
                                calculateFlag = true;
                                i = 0;
                            }
                        }
                        if (calculateFlag) {
                            i++;
                        }
                        if (text.contains("}") && !(text.contains("\"}")||(!text.contains("})") && text.contains("}\"")))) {
                            stringStack.pop();
                            calculateFlag = false;
                            if (stringStack.size() == 1 && (i-2) > LIMIT) {
                                System.out.println("文件：" + rootPath.getPath() + "存在超过"+LIMIT+"行的方法！");
                                FileOperUtil.writeTxtTotFile("D:\\log.txt","文件：" + rootPath.getPath() + "存在超过"+LIMIT+"行的方法！" + "\n");
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (null != inputStream) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (null != inputStreamReader) {
                        try {
                            inputStreamReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (null != bufferedReader) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            FileOperUtil.writeTxtTotFile("D:\\log.txt","---------------------end-----------------------\n");
            System.out.println("-----------------end------------------------");
        }
    }

}
